package com.beici.waterghost.controller;

import com.beici.waterghost.except.authenExcept;
import com.beici.waterghost.model.resultes;
import com.beici.waterghost.model.user;
import com.beici.waterghost.service.impl.commentServiceImpl;
import com.beici.waterghost.utils.contextUtils;
import com.beici.waterghost.utils.dateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther WaterGhost
 * @date 2022/1/28 18:28
 */
@RestController
@RequestMapping("/com")
@CrossOrigin
public class commentController {

    @Autowired
    private commentServiceImpl commentService;

    @PostMapping("/saveComment")
    public resultes saveComment(String context, Long nodeId, HttpServletRequest request){
        if (context.length()>30){
            return resultes.error("长度高于30");
        }
        boolean b = contextUtils.veifityContext(context);
        if (!b) {
            return resultes.error("内容出现敏感词汇");
        }
        try {
            user principal = (user) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            resultes resulte = commentService.saveComment(principal.getUserId(), context, request.getRemoteHost(), nodeId);
            if (resulte.getCode()==200){
                Map map = new HashMap();
                map.put("userName",principal.getUserName());
                map.put("userId",principal.getUserId());
                map.put("userHeader",principal.getUserHeader());
                map.put("comMess",context);
                map.put("comDate", dateUtils.getCurrentDetailsDate());
                return resultes.success("发布成功",map);
            }
            return resulte;
        }catch (Exception e){
            throw new authenExcept("尚未进行登录验证");
        }

    }

}
